import { addDiyPageInfo, updateDiyPageInfo } from '@/api/platform'
import { formpageAddEdit } from '@/api/merchant'
import { editThemeFestivalDiyPage } from '@/api/application'

import { Message } from 'element-ui'
const custorm = {
  state: {
    info: {},
    diy_page_id: null,
    setTop: null,
    class: {},
    form_id: null,
    textname: null,
    theme_festival_id:{},
   festival_page_id:{},
  },
  mutations: {
    SET_DIYPAGE_INFO: (state, info = []) => {
      const [$info] = info
      state.info = $info
    },
    SET_DIYPAGE_TOP: (state, signid) => {
      state.setTop = signid
    //   console.log(signid)
    },
    SET_DIYPAGE_ID: (state, signid) => {
      state.diy_page_id = signid
      // console.log(state.diy_page_id, 'state.diy_page_id')
    },
    SET_CLASS: (state, info = []) => {
      const [$info] = info
      state.class = $info
    },
    SET_THEME_FESTIVAL_ID:(state, info = [])=>{
      const [$info] = info
      state.theme_festival_id = $info
    },
    FESTIVAL_PAGE_ID:(state,info = [])=>{
      const [$info] = info
      state.festival_page_id = $info
    },
    SET_FORM_ID: (state, signid) => {
      state.form_id = signid
      // console.log(state.diy_page_id, 'state.diy_page_id')
    },
    SET_TEXTNAME: (state, signid) => {
      state.class.textname = signid
      console.log(state, signid, 'state.diy_page_id')
    }
  },
  actions: {
    addDiyPageInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        const { data } = state.info
        if (Array.isArray(data) && data.length > 2) {
          const { diy_page_id } = state.info
          console.log('=================+++++++++++++', state.info)

          const Base64 = require('js-base64').Base64
          const infodata = JSON.stringify(data)
          const storeBase = Base64.encode(infodata)
          const newSateinfo = Object.assign({}, state.info)
          newSateinfo.data = storeBase
          // 如果state下面存在diy_page_id就走修改页面这个接口，不然走保存接口
          if (diy_page_id) {
            updateDiyPageInfo(newSateinfo).then(res => {
              resolve(res)
            }).catch(error => {
              reject(error)
            })
          } else {
            // 装修页面保存页面请求的接口
            addDiyPageInfo(state.diy_page_id ? Object.assign({ diy_page_id: state.diy_page_id }, newSateinfo) : newSateinfo).then(res => {
              resolve(res)
            }).catch(error => {
              reject(error)
            })
          }
        } else {
          Message({
            message: '未设置自定义组件！~',
            type: 'error',
            duration: 2 * 1000
          })
        }
      })
    },
    toformpageAddEdit({ commit, state }) {
      return new Promise((resolve, reject) => {
        const { value, type, textname, form_id, classid } = state.class
        console.log('ddddddddddddddddpppppppppppp', state.class.textname)
        console.log('fffffffffff', type)
        if (form_id) {
          formpageAddEdit({ value, type: type, form_title: textname, form_id: form_id }).then(res => {
            resolve(res)
          }).catch(error => {
            reject(error)
          })
        } else {
          formpageAddEdit({ value, type: classid, form_title: textname }).then(res => {
            resolve(res)
          }).catch(error => {
            reject(error)
          })
        }
      })
    },
    toeditThemeFestivalDiyPage({ commit, state }){
      return new Promise((resolve, reject) => {
        const {value, theme_festival_id,festival_page_id} = state.theme_festival_id
        const Base64 = require('js-base64').Base64
        const infodata = JSON.stringify(value)
        const storeBase = Base64.encode(infodata)
        if(theme_festival_id){
          editThemeFestivalDiyPage({data:storeBase,theme_festival_id:theme_festival_id,festival_page_id:festival_page_id?festival_page_id:''}).then(res=>{
            resolve(res)
          }).catch(error => {
            reject(error)
          })
        }
      })
    }
  },
  getter: {

  }
}

export default custorm
